/*
 * RTSAI - Real-Time Strategy engine for AI research
 * Copyright(C) 2006 DIMAp-UFRN, licensed under LGPL
 *
 * Player implementation ( Player.cpp )
 */

#ifndef PLAYER_CPP_
#define PLAYER_CPP_

// INCLUDES ===================================================================
#include <iostream>
#include "Exceptions.h"
#include "Player.h"
#include "Action.h"
#include "GameView.h"
#include "Log.h"

// IMPLEMENTATIONS =============================================================
// PUBLIC METHODS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// string ----------------------------------------------------------------------
/*! This method returns the player's name
 *  @return Player's name.
 */
string Player :: get_name ()
{
    return this->name;
}


/*! This method returns the player's civilization name
 *  @return Player's civilization name.
 */
string Player :: get_civ_name ()
{
    return this->civilization;
}


// void ------------------------------------------------------------------------
/*! Activate the next unit in the player schedule
 */
void Player :: update ( const float elapsed_time )
{
    foreach ( u, units )
    {
        Action *action = (*u).act ();
        if (action && action -> check_precondition ( (*u) ) )
        {
            action -> execute ( (*u), elapsed_time );
        }
    }
}


/*! This method sets the player's name
 *  @param n The new player's name
 */
void Player :: set_name ( const string& n )
{
    this -> name = n;
}


/*! This method sets the player's civilization name
 *  @param n The new player's civilization name
 */
void Player :: set_civ_name ( const string& cn )
{
    this -> civilization = cn;
}


// Unit ------------------------------------------------------------------------
/*! Create a unit from a given class name
 *  @param class_name The unit class name
 */
Unit* Player :: create_unit ()
{
    Unit *u = &units.create ();
    u -> set_player ( this );
    return u;
}


/*! Called when a player, as a pool element, is destroyed. It implies in
 *  destroying their units too.
 */
void Player :: reset ()
{
    units.clear();
}


/*! Destructor
 */
Player :: ~Player () { /* Empty */ }


// Message related methods -----------------------------------------------------
/*! Distribute the received message to all units belonging to the player.
 */
void Player :: add_message ( Message *msg )
{
    foreach ( u, units )
        (*u).add_message ( msg );
}

#endif
